import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

// 抽象类: Tech
abstract class Tech {
    private String name;
    private int year;
    private String app;

    public Tech(String name, int year, String app) {
        this.name = name;
        this.year = year;
        this.app = app;
    }

    public abstract String describe();

    public String getName() {
        return name;
    }

    public int getYear() {
        return year;
    }

    public String getApp() {
        return app;
    }
}

// 接口: TechDetail
interface TechDetail {
    String detailDescription();
}

// 接口: EthicalView
interface EthicalView {
    String ethicalIssues();
}

// 接口: SocialView
interface SocialView {
    String socialImpact();
}

// 子类: AI (人工智能技术)
class AI extends Tech implements TechDetail, EthicalView, SocialView {
    public AI(String name, int year, String app) {
        super(name, year, app);
    }

    @Override
    public String describe() {
        return "人工智能技术";
    }

    @Override
    public String detailDescription() {
        return "人工智能是一种模拟人类智能的技术，通过机器学习和问题求解，可以用于语音识别、图像处理和自动驾驶等领域。";
    }

    @Override
    public String ethicalIssues() {
        return "人工智能可能引发一系列伦理问题，如隐私保护、人工智能产生的不平等和智能武器的使用等。";
    }

    @Override
    public String socialImpact() {
        return "人工智能对社会产生了广泛的影响，包括改变工作岗位、提高生产效率和推动社会发展等。";
    }
}

// 子类: Chain (区块链技术)
class Chain extends Tech implements TechDetail, EthicalView, SocialView {
    public Chain(String name, int year, String app) {
        super(name, year, app);
    }

    @Override
    public String describe() {
        return "区块链技术";
    }

    @Override
    public String detailDescription() {
        return "区块链是一种分布式、去中心化的数据库技术，通过密码学和共识算法确保数据的可信度和安全性。";
    }

    @Override
    public String ethicalIssues() {
        return "区块链技术引发诸多伦理问题，如权力集中问题、隐私保护和环境影响等。";
    }

    @Override
    public String socialImpact() {
        return "区块链技术对社会的影响主要体现在金融、供应链和社会治理等领域的改进和创新。";
    }
}

// 图形用户界面
class TechGUI extends JFrame {
    private HashMap<String, Tech> techMap;
    private JTextArea descriptionArea;
    private JTextArea detailArea;
    private JTextArea ethicalArea;
    private JTextArea socialArea;

    public TechGUI() {
        super("家族关系模拟------高辉");
        techMap = createTechData();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 左侧家族目录树
        JList<String> techList = new JList<>(techMap.keySet().toArray(new String[0]));
        techList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane listScrollPane = new JScrollPane(techList);
        listScrollPane.setPreferredSize(new Dimension(200, getHeight()));
        add(listScrollPane, BorderLayout.LINE_START);

        // 右侧展示信息
        JPanel infoPanel = new JPanel(new GridLayout(2, 2));

        descriptionArea = new JTextArea();
        descriptionArea.setEditable(false);
        JScrollPane descScrollPane = new JScrollPane(descriptionArea);
        descScrollPane.setBorder(BorderFactory.createTitledBorder("总体描述"));
        infoPanel.add(descScrollPane);

        detailArea = new JTextArea();
        detailArea.setEditable(false);
        JScrollPane detailScrollPane = new JScrollPane(detailArea);
        detailScrollPane.setBorder(BorderFactory.createTitledBorder("核心特点"));
        infoPanel.add(detailScrollPane);

        ethicalArea = new JTextArea();
        ethicalArea.setEditable(false);
        JScrollPane ethicalScrollPane = new JScrollPane(ethicalArea);
        ethicalScrollPane.setBorder(BorderFactory.createTitledBorder("伦理问题"));
        infoPanel.add(ethicalScrollPane);

        socialArea = new JTextArea();
        socialArea.setEditable(false);
        JScrollPane socialScrollPane = new JScrollPane(socialArea);
        socialScrollPane.setBorder(BorderFactory.createTitledBorder("社会影响"));
        infoPanel.add(socialScrollPane);

        add(infoPanel, BorderLayout.CENTER);

        // 注册点击事件
        techList.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                String selectedTech = techList.getSelectedValue();
                Tech tech = techMap.get(selectedTech);
                if (tech != null) {
                    descriptionArea.setText(tech.describe());
                    detailArea.setText(((TechDetail) tech).detailDescription());
                    ethicalArea.setText(((EthicalView) tech).ethicalIssues());
                    socialArea.setText(((SocialView) tech).socialImpact());
                }
            }
        });

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    // 创建技术数据
    private HashMap<String, Tech> createTechData() {
        AI ai = new AI("人工智能", 1956, "语音识别、图像处理、自动驾驶");
        Chain chain = new Chain("区块链", 2008, "金融、供应链、社会治理");

        HashMap<String, Tech> techMap = new HashMap<>();
        techMap.put(ai.getName(), ai);
        techMap.put(chain.getName(), chain);

        return techMap;
    }
}

// 测试代码
public class TechSimulation {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new TechGUI();
        });
    }
}